/*
 * GImage.java
 * Created on 16 juin 2007
 *
 * Glacéo Internet Platform
 * http://sourceforge.net/projects/chm/
 *
 * Copyright (c) 2005-2007, Jan Janke (VirtualHockey Project)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.hockeo.vhbase.data.dao.misc;


import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

import com.hockeo.vhbase.data.dao.IGEntity;

/**
 * Wraps around a binary image like a logo which is stored in the database and kept there
 * persistently. Only images considered as being part of the master data are represented
 * by objects of this class. In no case, this class should be used for short-term image
 * caching!
 *
 * @version $Id$
 * @author jjanke
 */
@Entity
@Table( name = "m_image" )
public class GImage implements IGEntity
{
  /**
   * Unique image id.
   */
  @Id
  @Column( name = "id" )
  @GeneratedValue( strategy = GenerationType.AUTO )
  private int    d_nId = 0;

  /**
   * GImage description.
   */
  @Column( name = "description" )
  private String d_strDescription;

  /**
   * Binary image data.
   */
  @Column( name = "data" )
  private byte[] d_data;

  /**
   * The image's content type, e.g. 'image/png'.
   */
  @Column( name = "mimetype" )
  private String d_strMimeType;

  /**
   * Size of the images sides in pixel.
   */
  @Column( name = "pixel" )
  private int    d_nPixel;

  /** Default no argument constructor. */
  protected GImage()
  {}

  /**
   * Returns the raw image data.
   */
  public byte[] getData()
  {
    return d_data;
  }

  /**
   * Returns the image's content type, e.g. 'image/png'.
   */
  public String getMimeType()
  {
    return d_strMimeType;
  }

  /**
   * Returns the image description. This is a usually a hint to the object that the image
   * refers to (e.g. a club code or a country code etc.). Nevertheless, other program code
   * should not rely on the information returned by this method since it may change from
   * one release to the next without prior notice. This method is ONLY for information and
   * debugging purposes.
   */
  public String getDescription()
  {
    return d_strDescription;
  }

  /**
   * Returns the id.
   */
  public int getId()
  {
    return d_nId;
  }

  public Class<? extends IGEntity> getGEntityClass()
  {
    return GImage.class;
  }

  public boolean instanceOf( Class<? extends IGEntity> classOther )
  {
    return classOther.isAssignableFrom( getGEntityClass() );
  }

  /*
   * @see java.lang.Object#equals(java.lang.Object)
   */
  @Override
  public boolean equals( Object obj )
  {
    try
    {
      return d_nId == ( (GImage) obj ).getId();
    }
    catch ( Exception ex ) // especially ClassCastException(s) may happen
    {
      return false;
    }
  }

  /*
   * @see java.lang.Object#hashCode()
   */
  @Override
  public int hashCode()
  {
    return d_nId;
  }

  /*
   * @see java.lang.Object#toString()
   */
  @Override
  public String toString()
  {
    return String.valueOf( d_nId ) + " (" + d_strDescription + ")";
  }
}
